package com.pai4j.search.messagequeue.consumer;
import com.pai4j.common.enums.MessageTypeEnum;
import com.pai4j.common.enums.ESMessageTypeEum;
import com.pai4j.common.enums.messagequeue.MessageQueueEnum;
import com.pai4j.common.service.messagequeue.consumer.AbstractRedisQueueConsumer;
import com.pai4j.common.util.JsonUtil;
import com.pai4j.domain.vo.dto.AbstractESMessageDTO;
import com.pai4j.domain.vo.response.subjectaccount.ArticleBaseResponseVO;
import com.pai4j.search.repository.entity.ChatMessageEntity;
import com.pai4j.search.service.ArticleService;
import com.pai4j.search.service.es.Pai4jElasticSearchService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;


/**
 * 聊天消息consumer
 * @author pai
 */
@Component
@Lazy(value = false)
@Slf4j
public class ChatMessageSyncRedisQueueConsumer extends AbstractRedisQueueConsumer<ChatMessageEntity> {


    @Resource
    private ArticleService articleService;

    @Resource
    private Pai4jElasticSearchService pai4jElasticSearchService;

    public void initBean() {
        new Thread(() -> schedule()).start();
    }


    @Override
    protected ChatMessageEntity convert(String s) {
        return JsonUtil.fromJson(s, ChatMessageEntity.class);
    }

    @Override
    protected MessageQueueEnum getMessageQueue() {

        return MessageQueueEnum.QUEUE_MESSAGE_SYNC_ES;
    }
    /**
     * 接收消息
     *
     * @param messageEntity 消息对象: 接收的是一个ChatMessageEntity对象
     * @throws Exception 抛出异常
     */
    @Override
    protected void receive(ChatMessageEntity messageEntity) throws Exception {
        pai4jElasticSearchService.sync(this.buildAbstractESMessageDTO(messageEntity));
    }


    /**
     * 构造ES消息索引数据
     *
     * @param chatMessageEntity 消息对象，用于构建ES消息索引数据
     * @return 返回构建好的AbstractESMessageDTO对象
     */
    private AbstractESMessageDTO buildAbstractESMessageDTO(ChatMessageEntity chatMessageEntity) {
        // 创建AbstractESMessageDTO对象
        AbstractESMessageDTO esMessage = new AbstractESMessageDTO();
        // 设置消息类型为聊天消息
        esMessage.setType(ESMessageTypeEum.CHAT_MESSAGE.getType());
        // 设置业务ID为消息实体的ID
        esMessage.setBizId(String.valueOf(chatMessageEntity.getId()));
        // 设置消息发送者ID
        esMessage.setSenderId(chatMessageEntity.getSender());
        // 设置消息接收者ID
        esMessage.setReceiverId(chatMessageEntity.getReceiver());
        // 设置消息代理发送者ID
        esMessage.setProxySenderId(chatMessageEntity.getProxySender());
        // 设置消息内容
        esMessage.setContent(chatMessageEntity.getMessage());
        // 将消息对象序列化为JSON字符串并设置为数据
        esMessage.setData(JsonUtil.toJsonString(chatMessageEntity));
        // 设置创建日期和更新日期为消息的发送日期
        esMessage.setCreateDate(chatMessageEntity.getSendDate());
        esMessage.setUpdateDate(chatMessageEntity.getSendDate());
        // 获取消息类型
        Integer type = chatMessageEntity.getType();
        // 如果消息类型为公众号文章
        if (MessageTypeEnum.SUBJECT_ACCOUNT_ARTICLE.getType().equals(type)) {
            // 公众号文章
            esMessage.setType(ESMessageTypeEum.SUBJECT_ACCOUNT_ARTICLE.getType());
            // 从缓存中获取文章详情
            ArticleBaseResponseVO article = articleService.detail(Long.valueOf(chatMessageEntity.getMessage()));
            // 方便文章搜索，支持标题+摘要搜索
            esMessage.setContent(article.getTitle() + ":" + article.getDescription());
        }
        // 返回构建好的ES消息对象
        return esMessage;
    }

    @Override
    protected void error(ChatMessageEntity data, Exception ex) {
        /**
         * 消费失败，重新放回队列
         */
        backToQueue(data);
        /**
         * 预警通知
         */
        log.error("ChatMessageSyncRedisQueueConsumer error", ex);
    }
    }

